import {Button, Loading} from '@antmjs/vantui'
import {View, ScrollView} from "@tarojs/components";
import {useCallback, useEffect,  useState} from 'react';
import Utils from "@/utils/common";
import LogUtils from "@/utils/log";
import { otherListApi} from "@/common/apis/home_datasource";
import DynamicFormV2 from "@/components/DynamicFormV2";
import './more_options.less'


// --- Component ---

export default function MoreOptions2(props) {
  const form = props.form;

  const [loading, setLoading] = useState(true);
  const [formConfig, setFormConfig] = useState<any[]>([]);

  const [, setRefresh] = useState({})

  const otherListApiInfo = useCallback(() => {
    if ( props?.id) {
      otherListApi(props?.id).then((res) => {
        if (res) {
          const formConfig2 =  Utils.transformApiDataToFormConfig(res, false)
          LogUtils.info('formConfig', formConfig2)
          setFormConfig(formConfig2)
        }
        setLoading(false);
        //延迟调用
        setTimeout(() => {
          form.setFields(props.moreOptions!)
          setRefresh({})
          LogUtils.info('moreOptions', props.moreOptions)
        }, 500)
      })
    }
  }, [form, props?.id, props.moreOptions]);
  useEffect(() => {
    otherListApiInfo()

  }, [form, otherListApiInfo]);


  if (loading) {
    return (
      <View style={{display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh'}}>
        <Loading type="spinner" size="24px">加载中...</Loading>
      </View>
    );
  }
  return (
    <View>
      <DynamicFormV2
        form={form}
        formConfigs={formConfig}
      />
    </View>
  )
}
